package org.dromara.demo.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseService;
import org.dromara.demo.domain.DemoExample2;
import org.dromara.demo.domain.DemoExample2Line;
import org.dromara.demo.service.DemoExample2ServiceImpl;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 测试主从
 * @author wenrui Su
 * @date 2024-03-24
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/demo/example2")
public class DemoExample2Controller extends BaseService {

  private final DemoExample2ServiceImpl testDemo2Service;

  /**
   * 查询测试主从列表
   */
  @SaCheckPermission("demo:example2:list")
  @GetMapping("/list")
  public TableDataInfo<DemoExample2> list(DemoExample2 bo, PageQuery pageQuery) {
    return testDemo2Service.queryPageList(bo, pageQuery);
  }

  /**
   * 导出测试主从列表
   */
  @SaCheckPermission("demo:example2:export")
  @Log(title = "测试主从", businessType = BusinessType.EXPORT)
  @PostMapping("/export")
  public void export(DemoExample2 bo, HttpServletResponse response) {
    List<DemoExample2> list = testDemo2Service.queryList(bo);
    ExcelUtil.exportExcel(list, "测试主从", DemoExample2.class, response);
  }

  /**
   * 获取测试主从详细信息
   * @param id 主键
   */
  @SaCheckPermission("demo:example2:query")
  @GetMapping("/{id}")
  public R<DemoExample2> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
    return R.ok(testDemo2Service.queryById(id));
  }

  /**
   * 新增测试主从
   */
  @SaCheckPermission("demo:example2:add")
  @Log(title = "测试主从", businessType = BusinessType.INSERT)
  @RepeatSubmit()
  @PostMapping()
  public R<Void> add(@Validated(AddGroup.class) @RequestBody DemoExample2 bo) {
    return toAjax(testDemo2Service.insertByBo(bo));
  }

  /**
   * 修改测试主从
   */
  @SaCheckPermission("demo:example2:edit")
  @Log(title = "测试主从", businessType = BusinessType.UPDATE)
  @RepeatSubmit()
  @PutMapping()
  public R<Void> edit(@Validated(EditGroup.class) @RequestBody DemoExample2 bo) {
    return toAjax(testDemo2Service.updateByBo(bo));
  }

  /**
   * 删除测试主从
   *
   * @param ids 主键串
   */
  @SaCheckPermission("demo:example2:remove")
  @Log(title = "测试主从", businessType = BusinessType.DELETE)
  @DeleteMapping("/{ids}")
  public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
    return toAjax(testDemo2Service.deleteWithValidByIds(List.of(ids), true));
  }

  /**
   * 查询测试从列表======================Line表的路由服务==========================
   */
  @SaCheckPermission("demo:example2:list")
  @GetMapping("/lineList")
  public TableDataInfo<DemoExample2Line> lineList(DemoExample2Line bo, PageQuery pageQuery) {
    return testDemo2Service.queryLinePageList(bo, pageQuery);
  }

  /**
   * 删除测试从
   * @param lineIds 主键串
   */
  @SaCheckPermission("demo:example2:remove")
  @Log(title = "测试从", businessType = BusinessType.DELETE)
  @DeleteMapping("/lineId/{lineIds}")
  public R<Void> removeLine(@NotEmpty(message = "主键不能为空") @PathVariable Long[] lineIds) {
    return toAjax(testDemo2Service.deleteWithValidByLineIds(List.of(lineIds), true));
  }
}
